package cn.asiawu.springboot.controller;

import cn.asiawu.springboot.common.Constants;
import cn.asiawu.springboot.common.Result;
import cn.asiawu.springboot.controller.dto.UserDto;
import cn.asiawu.springboot.entity.User;
import cn.asiawu.springboot.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {
//    @Autowired
//    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    //新增和修改
    @PostMapping
    public Result sava(@RequestBody User user) {
        return Result.success(userService.saveUser(user));
    }

    //查询所有数据
    @GetMapping("")
    public Result findAll() {
        return Result.success(userService.list());
    }

    //根据用户名查找用户信息
    @GetMapping("info/{username}")
    public Result findByUsername(@PathVariable String username) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return Result.success(userService.getOne(queryWrapper));
    }

    //总人数
    @GetMapping("/count")
    public Result getCount() {
        return Result.success(userService.count());
    }

    //删除一条数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(userService.removeById(id));
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(userService.removeBatchByIds(ids));
    }

    //mybatis-plus分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") Integer pageNum,
                               @RequestParam(defaultValue = "") Integer pageSize,
                               @RequestParam(defaultValue = "") String username,
                               @RequestParam(defaultValue = "") String email,
                               @RequestParam(defaultValue = "") String address) {
        Page<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (!"".equals(username))
            queryWrapper.like("username", username);
        if (!"".equals(email))
            queryWrapper.like("email", email);
        if (!"".equals(address))
            queryWrapper.like("address", address);
        return Result.success(userService.page(page, queryWrapper));
    }

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody UserDto userDto) {
        if (userDto.getUsername()==null||userDto.getPassword()==null||userDto.getPassword().equals("")||userDto.getUsername().equals(""))
            return Result.error(Constants.CODE_400,"参数错误");
        return Result.success(userService.login(userDto));
    }
    //注册
    @PostMapping("/register")
    public Result register(@RequestBody UserDto userDto) {
        if (userDto.getUsername()==null||userDto.getPassword()==null||userDto.getPassword().equals("")||userDto.getUsername().equals(""))
            return Result.error(Constants.CODE_400,"参数错误");
        return Result.success(userService.register(userDto));
    }
    //主动拦截接口
    @GetMapping("/activeFilter")
    public void activeFilter(){}

    //mybatis分页查询
//    @GetMapping("/page")
//    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
//                                        @RequestParam String username) {
//        pageNum=(pageNum-1)*pageSize;
//        List<User> data = userMapper.selectPage(pageNum, pageSize,username);
//        Integer total=userMapper.getTotal(username);
//        Map<String,Object> res=new HashMap<>();
//        res.put("data",data);
//        res.put("total",total);
//        return res;
//    }
}
